#include <cstdio>

int W[10][1005];
int C[10][1005];
int cases, N;
const int INF = 1000000000;
const int CLIMB = 60, HOLD = 30, SINK = 20;
const int HIGHEST = 9;

void initialize() {
    for(int i = 0; i <= N; ++i) {
        for(int j = HIGHEST; j >= 0; --j) {
            C[j][i] = INF;
        }
    }
}

void input() {
    for(int j = HIGHEST; j >= 0; --j) {
        for(int i = 0; i < N; ++i) {
            scanf("%d", &W[j][i]);
        }
    }
}
int cal() {
    for(int i = 1; i <= N; ++i) {
        if(i == 1) {
            C[1][1] = CLIMB - W[0][0];
        } else if(i < N) {
            for(int j = 1; j <= HIGHEST; ++j) {
                for(int k = j - 1; k <= j + 1; ++k) {
                    if(0 < k && k <= HIGHEST && C[k][i - 1] != INF) {
                       if(k == j - 1 && C[j][i] > C[k][i - 1] + CLIMB - W[k][i - 1]) {
                           C[j][i] = C[k][i - 1] + CLIMB - W[k][i - 1];
                       } else if(k == j && C[j][i] > C[k][i - 1] + HOLD - W[k][i - 1]) {
                           C[j][i] = C[k][i - 1] + HOLD - W[k][i - 1];
                       } else if(k == j + 1 && C[j][i] > C[k][i - 1] + SINK - W[k][i - 1]) {
                           C[j][i] = C[k][i - 1] + SINK - W[k][i - 1];
                       }
                    }
                }
            }
        } else {
            C[0][N] = C[1][N - 1] + SINK - W[1][N - 1];
        }
    }
    return C[0][N];
}

void output() {
    printf("%d\n\n", cal());
}

int main() {
    scanf("%d", &cases);
    while(cases--) {
        scanf("%d", &N);
        N /= 100;
        initialize();
        input();
        output();
    }
    return 0;
}
